import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/base_plugin/base_dialog.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/widgets/bottom_sheet/sort_transaction_bottom_sheet.dart';
import 'package:flutter/material.dart';

mixin TimeSortMixin<T> on BaseProvider<T> {
  String currentSelectTime;

  DateTime currentStartDate;
  DateTime currentEndDate;

  @override
  void initState() {
    currentSelectTime = S.current.all_time;
    super.initState();
  }

  String get startTime {
    if (currentSelectTime == S.current.all_time) {
      return null;
    } else if (currentSelectTime == S.current.current_month) {
      final current = DateTime.now();
      return DateTimeUtils.formatDateTime(
          DateTime(current.year, current.month, 1), 'yyyy-MM-dd');
    } else if (currentSelectTime == S.current.last_3_months) {
      return DateTimeUtils.formatDateTime(
          DateTimeUtils.getLast3Month(), 'yyyy-MM-dd');
    } else {
      return DateTimeUtils.formatDateTime(currentStartDate, 'yyyy-MM-dd');
    }
  }

  String get endTime {
    if (currentSelectTime == S.current.all_time) {
      return null;
    } else if (currentSelectTime == S.current.current_month ||
        currentSelectTime == S.current.last_3_months) {
      final current = DateTime.now();
      return DateTimeUtils.formatDateTime(
          DateTime(current.year, current.month, current.day), 'yyyy-MM-dd');
    } else {
      return DateTimeUtils.formatDateTime(currentEndDate, 'yyyy-MM-dd');
    }
  }

  String get rangeTime => currentStartDate != null && currentEndDate != null
      ? '${DateTimeUtils.formatDateTime(currentStartDate, 'd MMM yyyy')} - ${DateTimeUtils.formatDateTime(currentEndDate, 'd MMM yyyy')}'
      : null;

  void unFocus();

  Future<void> onRefresh();

  void onSelectTime() {
    unFocus();
    obtainContext?.call((context) async {
      final result = await showSortTransactionBottomSheet(
          context, currentSelectTime, currentStartDate, currentEndDate);
      if (result is Map) {
        if (currentSelectTime != result['type'] ||
            currentStartDate != result['startDate'] ||
            currentStartDate != result['endDate']) {
          currentSelectTime = result['type'];
          currentStartDate = result['startDate'];
          currentEndDate = result['endDate'];
          update();
          onRequestRefresh();
        }
      } else {
        if (currentSelectTime != result) {
          currentSelectTime = result;
          update();
          onRequestRefresh();
        }
      }
    });
  }

  void onRequestRefresh() {
    obtainContext?.call((context) async {
      BaseDialog().showLoadingDialog(context: context, text: S.current.loading);
      await onRefresh();
      Navigator.of(context).pop();
    });
  }

  void onClearRangeTime() {
    currentStartDate = null;
    currentEndDate = null;
    currentSelectTime = S.current.all_time;
    update();
    onRequestRefresh();
  }
}
